package cn.tedu.lucky.draw.server.controller;

import cn.tedu.lucky.draw.server.common.annotation.OperationLog;
import cn.tedu.lucky.draw.server.common.result.JsonResult;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.protocol.po.Prize;
import cn.tedu.lucky.draw.server.protocol.query.ActivityQuery;
import cn.tedu.lucky.draw.server.service.IActivityService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 控制层-抽奖活动
 */
@Slf4j
@RestController
@CrossOrigin
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job csvToDBJob;

    @Autowired
    private JobExplorer jobExplorer;

    /**
     * 获取活动列表
     */
    @PostMapping("/draw/activty/getAll")
    public JsonResult<List<Activity>> getAll(@RequestBody ActivityQuery activityQuery){
        List<Activity> list = activityService.getAll(activityQuery);
        return JsonResult.ok(list);
    }

    /**
     * 新增抽奖活动
     */
    @OperationLog(operation = "新增活动")
    @PostMapping("/draw/activty/save")
    public JsonResult<Void> saveActivity(@RequestBody Activity activity){
        log.debug("Controller-新增抽奖活动-入参-activity:{}",activity);
        activityService.saveActivity(activity);
        log.debug("Controller-新增抽奖活动-出参:{}",JsonResult.ok());
        return JsonResult.ok();
    }

    /**
     * 使用SpringBatch将活动数据从csv文件导入到数据库
     * http://localhost:9090/activty/csvToDB
     */
    @GetMapping("/activty/csvToDB")
    public String csvToDB() throws Exception {
        JobParameters jobParameters = new JobParametersBuilder(new JobParameters(), jobExplorer)
                .addLong("time", new Date().getTime())//记录当前操作时间
                //获取下一个JobParameters
                .getNextJobParameters(csvToDBJob).toJobParameters();
        JobExecution run = jobLauncher.run(csvToDBJob, jobParameters);
        return run.getId().toString();
    }

    /**
     * 获取活动详情
     */
    @PostMapping("/draw/activty/{id}")
    public JsonResult<Activity> getActivtyById(@PathVariable Long id){
        Activity activity = activityService.findActivityById(id);
        return JsonResult.ok(activity);
    }

    @ApiOperation("app获取活动列表")
    @GetMapping("/app/draw/activity/getAll")
    public JsonResult<List<Activity>> getAll(){
        return JsonResult.ok(
                activityService.findAll()
        );
    }

    /**
     * 触发抽奖
     */
    @ApiOperation("app触发抽奖")
    @GetMapping("/app/draw/activity/trigger")
    public JsonResult<Prize> trigger(Long activityId) throws JSONException {
        Prize prize = activityService.trigger(activityId);
        return JsonResult.ok(prize);
    }

}
